# -*- coding: utf-8 -*-

"""
* Copyright (C) 2016 GridSafe, Inc.
"""

import datetime
import pytz
import pdb
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.conf import settings
import re
from apps.wallet.logging import view_log
from django.views.generic import TemplateView


# 当前的时间函数 alias
def dt_now():
    return datetime.datetime.now(tz=pytz.timezone(settings.TIME_ZONE))


def get_pages(page, paginator):
    """返回一个 pages list,用于展示的页码"""
    try:
        page = int(page)
    except:
        page = 1
    if page <= 6:
        # 显示 1 - 10 页
        pages = paginator.page_range[:10]
    else:
        pages = paginator.page_range[page - 5:page + 5]

    return pages


class HomepageView(TemplateView):
    template_name = 'app/homepage.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        view_log.info('some one enter homepage')
        return context

    def get_template_names(self):
        user_agent = self.request.META.get('HTTP_USER_AGENT')
        result = re.search('Android|webOS|iPhone|iPad|iPod|Opera Mini',
                           user_agent)
        if result:
            return 'app/mobile-homepage.html'
        else:
            return self.template_name
